package com.hnx.admin.aurora.core.func;

import com.hnx.admin.aurora.annotation.AddInfo;
import com.hnx.admin.aurora.core.SafeInsertSqlBuilder;
import com.hnx.admin.aurora.core.SqlWithParams;
import com.hnx.admin.aurora.core.api.BuildSqlStrategy;
import com.hnx.admin.aurora.security.utils.SecurityUtil;
import com.hnx.admin.aurora.system.domain.vo.user.entity.UserDetailVO;
import com.hnx.admin.aurora.system.domain.vo.user.entity.UserVO;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/*
创建人： 倔强的头发
创建时间：2025/7/3
功能描述:

*/
public class BuildInsertSql implements BuildSqlStrategy {
    @Override
    public SqlWithParams process(Method method, Object obj) {
        AddInfo annotation = method.getAnnotation(AddInfo.class);
        if (Objects.nonNull(annotation)) {
            UserDetailVO userDetailVO = (UserDetailVO) SecurityUtil.getPrincipal();
            if (Objects.nonNull(userDetailVO)) {
                Object id = getId(obj);
                if (Objects.nonNull(id)) {
                    UserVO userVO = userDetailVO.getUserVO();
                    Map<String, Object> param = new HashMap<>();
                    param.put(ID, id);
                    param.put(CREATE_BY, userVO.getUserId());
                    param.put(USER_ID, userVO.getUserId());
                    param.put(DEPT_ID, userVO.getEmp().getDeptId());
                    SafeInsertSqlBuilder builder = new SafeInsertSqlBuilder()
                            .into(TABLE)
                            .value(ID, param.get(ID))
                            .value("create_by", param.get(CREATE_BY))
                            .valueExpression("create_time", "sysdate()")
                            .value("user_id", param.get(USER_ID))
                            .value("dept_id", param.get(DEPT_ID));
                    return builder.build();
                }
            }
        }
        return null;
    }
}
